package com.colin.aiyi.ui.Home.chudao;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.colin.aiyi.R;
import com.colin.aiyi.adapter.MeAdapter;
import com.colin.aiyi.app.MyApp;
import com.colin.aiyi.dao.ImpxUtils.ImpAudioDetail;
import com.colin.aiyi.dao.ImpxUtils.ImpAudioSave;
import com.colin.aiyi.dao.ImpxUtils.ImpAudioUpdate;
import com.colin.aiyi.dao.ImpxUtils.ImpGetListByCode;
import com.colin.aiyi.entity.Chudao;
import com.colin.aiyi.entity.response.RoleResponse;
import com.colin.aiyi.entity.User;
import com.colin.aiyi.presenter.AbsP;
import com.colin.aiyi.utils.SharePreferenceUtil;
import com.colin.aiyi.view.ShowMsgUtil;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by hl on 2017/6/5.
 */

public class ImpPYiMing extends AbsP {
    IViewReferrer iviewReferrer;
    Chudao chudao=new Chudao();
    MeAdapter meAdapter;
    private User user= SharePreferenceUtil.getObject(MyApp.getInstance().getApplicationContext(),User.class);

    private  static  ImpPYiMing impYiMing;
    public static ImpPYiMing getInstance(IViewReferrer iview) {
        if (impYiMing==null){
            impYiMing=new ImpPYiMing();
            impYiMing.setIviewReferrer(iview);
        }
        return impYiMing;
    }

    public void setIviewReferrer(IViewReferrer iviewReferrer) {
        this.iviewReferrer = iviewReferrer;
    }

    @Override
    public void onSuccess(Object t) {
        super.onSuccess(t);
        ShowMsgUtil.hideLoadingDialog();
        if (iviewReferrer!=null){
            iviewReferrer.refreshStatus(t );
        }
    }

    @Override
    public <V> void audio_detail(V v) {
        super.audio_detail(v);
        iService=new  ImpAudioDetail();
        iService.getObject(v);
    }


    @Override
    public <V> void audio_save(V v) {
        super.audio_save(v);
        iService=new ImpAudioSave();
        iService.getObject(this,v);
    }

    @Override
    public <V> void audio_update(V v) {
        super.audio_update(v);
        iService=new ImpAudioUpdate();
        iService.getObject(this,v);
    }


    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        super.onItemChildClick(adapter, view, position);
            Chudao.UrlsBean urlsBean=new Chudao.UrlsBean();
            adapter.getData().add(urlsBean);
            adapter.notifyItemInserted(adapter.getData().size());
//        if (position+1==adapter.getData().size()){
//            ((Chudao.UrlsBean)adapter.getData().get(position)).setLast(false);
//            Chudao.UrlsBean urlsBean=new Chudao.UrlsBean();
//            urlsBean.setLast(true);
//            view.getBackground().setLevel(0);
//            adapter.getData().add(urlsBean);
//            adapter.notifyItemInserted(adapter.getData().size());
//        }else {
//            if (position+1>adapter.getData().size())
//                return;
//            try {
//                view.getBackground().setLevel(0);
//                adapter.getData().remove(position);
//                adapter.notifyItemRemoved(position);
//            } catch (Exception e) {
//                e.printStackTrace();
//
//            }
//        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        super.onItemClick(adapter, view, position);
        RoleResponse.DataBean dataBean= (RoleResponse.DataBean) adapter.getData().get(position);
        for (Object data : adapter.getData()) {
            ((RoleResponse.DataBean)data).setSelcet(false);
        }
        dataBean.setSelcet(true);
       adapter.notifyDataSetChanged();
        chudao.setRoleId(dataBean.getId());
        chudao.setRoleName(dataBean.getParamName());
        Set<String > set=new HashSet<>();
        set.add(chudao.getRoleName());
        set.add(chudao.getRoleId()+"");
        JPushInterface.setTags(view.getContext(),set,null);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {

            case R.id.button2:
                String ddf=iviewReferrer.getContent();
                if (ddf==null||ddf.length()<1)
                {
                    iviewReferrer.showMsg("请输入简介内容");
                    return;
                }
                meAdapter=iviewReferrer.getAdapter();

                StringBuffer stringBuffer = new StringBuffer();
                for (int j = 0; j < meAdapter.getData().size(); j++) {
                    String url = meAdapter.getData().get(j).getUrl();
                    if (url != null && url.length() > 1) {
                        stringBuffer.append(url);
                        if (j + 1 < meAdapter.getData().size())
                            stringBuffer.append(",");
                    }
                }
                if (stringBuffer.toString().length() < 1) {
                    iviewReferrer.showMsg("请输入作品链接");
                    return;
                }
                chudao.setContent(ddf);
                chudao.setStrUrls(stringBuffer.toString());
                chudao.setAccessToken(user.getAccessToken());
                if (iviewReferrer.isUpadate()){
                   audio_update(chudao);
                }else {
                    audio_save(chudao);
                }
                ShowMsgUtil.showLoadingDialog(v.getContext());
                break;
        }
    }

    @Override
    public <V> void base_getListByCode(V v) {
        super.base_getListByCode(v);
        iService=new ImpGetListByCode();
        iService.getObject(v);
    }
}
